package com.jrx.anytxn.transaction.service.impl;

import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.dto.transactionLog.TlPaymentMappingRes;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import com.jrx.anytxn.transaction.entity.AmLoanMainInfo;
import com.jrx.anytxn.transaction.entity.TlPaymentAllocationLog;
import com.jrx.anytxn.transaction.entity.TlPaymentMapping;
import com.jrx.anytxn.transaction.mapper.ext.ExtAmLoanAccountInfoMapper;
import com.jrx.anytxn.transaction.mapper.ext.ExtAmLoanMainInfoMapper;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlPaymentAllocationLogMapper;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlPaymentMappingMapper;
import com.jrx.anytxn.transaction.service.IPaymentMappingService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 还款流水与订单关联service实现
 *
 * @author yf
 * date 2020/7/28 0028
 * @version V1.0
 **/
@Service
public class PaymentMappingServiceImpl implements IPaymentMappingService {

    @Autowired
    private ExtTlPaymentMappingMapper extTlPaymentMappingMapper;
    @Autowired
    private ExtAmLoanAccountInfoMapper extAmLoanAccountInfoMapper;
    @Autowired
    private ExtAmLoanMainInfoMapper extAmLoanMainInfoMapper;
    @Autowired
    private ExtTlPaymentAllocationLogMapper extTlPaymentAllocationLogMapper;

    @Override
    public List<TlPaymentMappingRes> getPaymentMappingBySeqNoAndCusId(String tenantId, String customerId, String seqNo) {
        List<TlPaymentMapping> tlPaymentMappingList = extTlPaymentMappingMapper.selectByPaymentSeqAndCustomerId(seqNo, customerId, tenantId);
        List<TlPaymentMappingRes> res = new ArrayList<>();
        for (TlPaymentMapping tlPaymentMapping : tlPaymentMappingList) {
            TlPaymentMappingRes tlPaymentMappingRes = new TlPaymentMappingRes();
            //查询此订单信息
            AmLoanMainInfo amLoanMainInfo = extAmLoanMainInfoMapper.selectByLoanIdAndCusId(tlPaymentMapping.getLoanId(), customerId, tenantId);
            //查询此订单账户信息
            AmLoanAccountInfo amLoanAccountInfo = extAmLoanAccountInfoMapper.selectByLoanId(tlPaymentMapping.getLoanId(), customerId, tenantId);

            List<TlPaymentAllocationLog> custLog = extTlPaymentAllocationLogMapper.findCustLog(amLoanMainInfo.getLoanId(), seqNo, amLoanMainInfo.getCustomerId(), amLoanMainInfo.getTenantId());
            if (CollectionUtils.isNotEmpty(custLog)){
                // 只查询09的交易账户，过滤掉08的交易账户
                custLog.stream().filter((TlPaymentAllocationLog log) -> TransBizConstant.PAYMENT_ALLOCATION_TYPE_09.equalsIgnoreCase(log.getAccountType())).collect(Collectors.toList());
            }
            BigDecimal paymentAmount = custLog.stream().map(TlPaymentAllocationLog::getPostingAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

            tlPaymentMappingRes.setPaymentAmount(paymentAmount);
            tlPaymentMappingRes.setCustomerId(customerId);
            tlPaymentMappingRes.setLoanId(tlPaymentMapping.getLoanId());
            tlPaymentMappingRes.setAccountStatus(amLoanAccountInfo.getAccountStatus());
            tlPaymentMappingRes.setChannel(amLoanAccountInfo.getChannel());
            tlPaymentMappingRes.setCompelCollectionFlag(amLoanAccountInfo.getCompelCollectionFlag());
            tlPaymentMappingRes.setContractId(amLoanMainInfo.getContractId());
            tlPaymentMappingRes.setContractNumber(amLoanMainInfo.getContractNumber());
            tlPaymentMappingRes.setLoanType(amLoanMainInfo.getLoanType());
            tlPaymentMappingRes.setCreateTime(tlPaymentMapping.getCreateTime());
            tlPaymentMappingRes.setLoanAmount(amLoanMainInfo.getLoanAmount());
            res.add(tlPaymentMappingRes);

        }
        return res;
    }

    @Override
    public List<TlPaymentMappingRes> getPaymentMappingByCusIdAndLoanId(String tenantId, String customerId, String loanId) {
        List<TlPaymentMappingRes> res = new ArrayList<>();
        TlPaymentMappingRes tlPaymentMappingRes = new TlPaymentMappingRes();
        //查询此订单信息
        AmLoanMainInfo amLoanMainInfo = extAmLoanMainInfoMapper.selectByLoanIdAndCusId(loanId, customerId, tenantId);
        //查询此订单账户信息
        AmLoanAccountInfo amLoanAccountInfo = extAmLoanAccountInfoMapper.selectByLoanId(loanId, customerId, tenantId);
        if (amLoanMainInfo != null && amLoanAccountInfo != null) {
            tlPaymentMappingRes.setCustomerId(customerId);
            tlPaymentMappingRes.setLoanId(loanId);
            tlPaymentMappingRes.setAccountStatus(amLoanAccountInfo.getAccountStatus());
            tlPaymentMappingRes.setChannel(amLoanAccountInfo.getChannel());
            tlPaymentMappingRes.setCompelCollectionFlag(amLoanAccountInfo.getCompelCollectionFlag());
            tlPaymentMappingRes.setContractId(amLoanMainInfo.getContractId());
            tlPaymentMappingRes.setContractNumber(amLoanMainInfo.getContractNumber());
            tlPaymentMappingRes.setLoanType(amLoanMainInfo.getLoanType());
            res.add(tlPaymentMappingRes);
        }
        return res;
    }
}